solve day 10
This commit is contained in:
parent
bf5d3c1656
commit
c93c273ffd
|
|
@ -9,5 +9,6 @@ let () =
|
||||||
Day6.print ();
|
Day6.print ();
|
||||||
Day7.print ();
|
Day7.print ();
|
||||||
Day8.print ();
|
Day8.print ();
|
||||||
Day9.print ()
|
Day9.print ();
|
||||||
|
Day10.print ()
|
||||||
;;
|
;;
|
||||||
|
|
|
||||||
143
inputs/day10.tt
Normal file
143
inputs/day10.tt
Normal file
|
|
@ -0,0 +1,143 @@
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -5
|
||||||
|
addx 6
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -38
|
||||||
|
addx 41
|
||||||
|
addx -22
|
||||||
|
addx -14
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -12
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -16
|
||||||
|
addx 17
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -30
|
||||||
|
noop
|
||||||
|
addx -6
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -12
|
||||||
|
addx 17
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx 10
|
||||||
|
addx -9
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -5
|
||||||
|
addx 6
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -37
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -12
|
||||||
|
addx 30
|
||||||
|
addx -23
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -17
|
||||||
|
addx 22
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
addx 11
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -2
|
||||||
|
noop
|
||||||
|
addx -6
|
||||||
|
addx -29
|
||||||
|
addx 37
|
||||||
|
addx -30
|
||||||
|
addx 27
|
||||||
|
addx -2
|
||||||
|
addx -22
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -5
|
||||||
|
addx 6
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -25
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
146
inputs/day11.tt
Normal file
146
inputs/day11.tt
Normal file
|
|
@ -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
|
||||||
48
src/day10.ml
Normal file
48
src/day10.ml
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
open Containers
|
||||||
|
|
||||||
|
let parse_lines () =
|
||||||
|
let lines = Utils.lines_of_input 10 in
|
||||||
|
let _, state_history =
|
||||||
|
List.fold_flat_map
|
||||||
|
(fun (cycle, reg) instruction ->
|
||||||
|
match String.split ~by:" " instruction with
|
||||||
|
| [ "noop" ] -> (cycle + 1, reg), [ cycle + 1, reg ]
|
||||||
|
| [ "addx"; num ] ->
|
||||||
|
let num = int_of_string num in
|
||||||
|
(cycle + 2, reg + num), [ cycle + 1, reg; cycle + 2, reg ]
|
||||||
|
| _ -> failwith @@ Printf.sprintf "failed to parse '%s'" instruction)
|
||||||
|
(0, 1)
|
||||||
|
lines
|
||||||
|
in
|
||||||
|
state_history
|
||||||
|
;;
|
||||||
|
|
||||||
|
let part1 () =
|
||||||
|
let history = parse_lines () in
|
||||||
|
history
|
||||||
|
|> List.find_all (fun (cycle, _) ->
|
||||||
|
match cycle with
|
||||||
|
| 20 | 60 | 100 | 140 | 180 | 220 -> true
|
||||||
|
| _ -> false)
|
||||||
|
|> List.fold_left (fun acc (cycle, reg) -> acc + (cycle * reg)) 0
|
||||||
|
;;
|
||||||
|
|
||||||
|
let part2 () =
|
||||||
|
let screen_width = 40 in
|
||||||
|
let history = parse_lines () in
|
||||||
|
history
|
||||||
|
|> List.map (fun (cycle, reg) ->
|
||||||
|
let column = Int.(cycle mod screen_width) in
|
||||||
|
let sprite_dist = abs @@ (reg + 1 - column) in
|
||||||
|
if sprite_dist <= 1 then '#' else ' ')
|
||||||
|
|> List.chunks 40
|
||||||
|
|> List.map String.of_list
|
||||||
|
|> List.iter Fun.(print_string %> print_newline)
|
||||||
|
;;
|
||||||
|
|
||||||
|
let print () =
|
||||||
|
Printf.(
|
||||||
|
printf "Day 10.1: %d\n" @@ part1 ();
|
||||||
|
printf "Day 10.2:\n";
|
||||||
|
part2 ())
|
||||||
|
;;
|
||||||
Loading…
Reference in New Issue
Block a user