~ihabunek/aoc2022

b84630ebcdf15351b5734da1904097f4ef2ca06b — Ivan Habunek 1 year, 9 months ago b9d16fc
Day 10
A input/day10.example1.txt => input/day10.example1.txt +3 -0
@@ 0,0 1,3 @@
noop
addx 3
addx -5
\ No newline at end of file

A input/day10.example2.txt => input/day10.example2.txt +146 -0
@@ 0,0 1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop
\ No newline at end of file

A input/day10.txt => input/day10.txt +137 -0
@@ 0,0 1,137 @@
noop
noop
addx 5
addx 1
addx 10
addx -4
noop
addx -1
noop
addx 5
addx -5
addx 9
addx 2
addx -15
addx 18
addx 8
addx -2
noop
addx -18
addx 21
addx 1
addx -37
addx 27
addx -24
addx 2
addx 5
addx -7
addx 26
addx -16
addx 2
addx 5
addx -15
noop
addx 20
addx 2
addx 4
addx -3
addx 2
noop
addx 3
addx 2
addx 5
addx -40
addx 2
addx 33
addx -30
addx 5
addx 5
addx 17
addx -19
addx 2
addx 5
addx 20
addx -16
addx 3
addx -2
addx 7
noop
addx -2
addx 5
addx 2
addx 3
addx -2
addx -38
addx 5
addx 2
addx 1
addx 15
addx -8
noop
addx -2
addx 4
addx 2
addx 4
addx -2
noop
addx 6
addx 2
addx -1
addx 4
noop
addx 1
addx 4
noop
noop
noop
addx -37
addx 5
addx 2
addx 22
addx -17
addx -2
noop
addx 3
addx 2
noop
addx 3
addx 2
noop
noop
noop
addx 5
addx 5
addx 2
addx 3
noop
addx 2
addx -23
addx 2
addx -14
noop
addx 29
addx -26
noop
addx 8
noop
noop
noop
addx -9
addx 11
addx 5
addx 2
noop
addx 1
noop
noop
addx 5
noop
noop
addx 2
noop
addx 3
addx 2
addx -2
noop
noop
noop
\ No newline at end of file

M src/aoc2022.gleam => src/aoc2022.gleam +3 -3
@@ 1,9 1,9 @@
import gleam/io
import aoc2022/day09
import aoc2022/day10

pub fn main() {
  io.print("Part 1: ")
  io.debug(day09.part1())
  io.debug(day10.part1())
  io.print("Part 2: ")
  io.debug(day09.part2())
  io.debug(day10.part2())
}

A src/aoc2022/day10.gleam => src/aoc2022/day10.gleam +71 -0
@@ 0,0 1,71 @@
//// --- Day 10: Cathode-Ray Tube ---
//// https://adventofcode.com/2022/day/10
//// Activate next instruction

import aoc2022/utils
import gleam/list
import gleam/string

type State {
  State(idx: Int, x: Int, sum: Int)
}

pub fn part1() {
  let state =
    utils.read_input("day10.txt")
    |> string.split("\n")
    |> list.flat_map(fn(line) {
      case line {
        "addx " <> n -> [0, utils.parse_int(n)]
        "noop" -> [0]
      }
    })
    |> list.fold(
      State(1, 1, 0),
      fn(state, dx) {
        let idx = state.idx + 1
        let x = state.x + dx

        let sum = case { idx - 20 } % 40 {
          0 -> state.sum + x * idx
          _ -> state.sum
        }

        State(idx, x, sum)
      },
    )

  state.sum
}

pub fn part2() {
  let state =
    utils.read_input("day10.txt")
    |> string.split("\n")
    |> list.flat_map(fn(line) {
      case line {
        "addx " <> n -> [0, utils.parse_int(n)]
        "noop" -> [0]
      }
    })
    |> list.fold(
      #(0, 1, ["#"]),
      fn(state, dx) {
        let idx = { state.0 + 1 } % 40
        let x = state.1 + dx

        let char = case idx >= x - 1 && idx <= x + 1 {
          True -> "#"
          False -> "."
        }

        #(idx, x, [char, ..state.2])
      },
    )

  state.2
  |> list.reverse
  |> list.sized_chunk(40)
  |> list.map(string.join(_, ""))
  |> string.join("\n")
}