-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC Day 10.m
35 lines (26 loc) · 891 Bytes
/
AoC Day 10.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(* ::Package:: *)
(* ::Text:: *)
(*Written December 10th, 2022.*)
(*Import*)
day = 10;
inputPath = FileNameJoin[{NotebookDirectory[], "Day" <> ToString[day] <> "Input.txt"}];
toExpression[inputText_] :=
Map[
If[! IntegerQ[#] \[And]
StringMatchQ[#,
Alternatives["+", "-", ""] ~~ DigitCharacter ..],
ToExpression[#], #] &,
inputText,
{Depth[inputText] - 1, Depth[inputText]}];
input = toExpression[Import[inputPath, "Table"]];
(*Parts 1 & 2*)
newInput = input /. {"addx", x : _} :> Sequence[{"noop"}, {"addx", x}];
cycles = {20, 60, 100, 140, 180, 220};
strength = 1; part1 = 0;
screen = Table[0, {i, 240}];
Do[
If[newInput[[i, 1]] == "addx", strength += newInput[[i, 2]]];
If[Abs[Mod[i, 40] - strength] <= 1, screen[[i + 1]] = 1];
If[MemberQ[cycles, i], part1 += strength*i];,
{i, Length[newInput]}];
{part1, ArrayPlot[Partition[screen, 40]]}