summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--day3/__init__.py94
-rw-r--r--day3/example.txt10
-rw-r--r--day3/input.txt140
3 files changed, 244 insertions, 0 deletions
diff --git a/day3/__init__.py b/day3/__init__.py
new file mode 100644
index 0000000..ebe243e
--- /dev/null
+++ b/day3/__init__.py
@@ -0,0 +1,94 @@
1# -*- coding: utf-8 -*-
2from abc import ABC
3from functools import reduce
4from typing import Optional, Iterator
5
6from aoc import BaseAssignment, I, T
7from aoc.datastructures import Coordinate
8
9
10class Assignment(BaseAssignment, ABC):
11 def symbol_overlap(self, number_coordinates: list[Coordinate], symbols: set[Coordinate]) -> set[Coordinate]:
12 neighbours = {
13 neighbour
14 for coordinate in number_coordinates
15 for neighbour in coordinate.neighbours()
16 }
17
18 return neighbours & symbols
19
20 def read_input(self, example=False) -> tuple[list[str], list[tuple[list[Coordinate], int]], set[Coordinate]]:
21 schematic = list(super().read_input(example))
22 numbers: list[tuple[list[Coordinate], int]] = list()
23 symbols: set[Coordinate] = set()
24
25 for y, row in enumerate(schematic):
26 number: Optional[list[list[Coordinate], str]] = None
27
28 for x, item in enumerate(row):
29 if item.isdigit():
30 if number is None:
31 number = [[], '']
32
33 number[0].append(Coordinate(x, y))
34 number[1] += item
35 continue
36
37 if number is not None:
38 numbers.append((number[0], int(number[1])))
39 number = None
40
41 if item != '.':
42 symbols.add(Coordinate(x, y))
43
44 if number is not None:
45 numbers.append((number[0], int(number[1])))
46
47 return schematic, numbers, symbols
48
49
50class AssignmentOne(Assignment):
51 example_result = 4361
52
53 def is_part_number(self, number_coordinates: list[Coordinate], symbols: set[Coordinate]) -> bool:
54 return len(self.symbol_overlap(number_coordinates, symbols)) > 0
55
56 def run(self, input: tuple[list[tuple[list[Coordinate], int]], set[Coordinate]]) -> int:
57 _, numbers, symbols = input
58
59 part_numbers = []
60
61 for coordinates, number in numbers:
62 if self.is_part_number(coordinates, symbols):
63 part_numbers.append(number)
64
65 return sum(part_numbers)
66
67
68class AssignmentTwo(Assignment):
69 example_result = 467835
70
71 def run(self, input: tuple[list[tuple[list[Coordinate], int]], set[Coordinate]]) -> int:
72 schematic, numbers, symbols = input
73
74 possible_gears = {}
75
76 for coordinates, number in numbers:
77 for symbol in self.symbol_overlap(coordinates, symbols):
78 if schematic[symbol.y][symbol.x] != '*':
79 continue
80
81 if symbol not in possible_gears:
82 possible_gears[symbol] = []
83
84 possible_gears[symbol].append(number)
85
86 return sum([
87 reduce(
88 lambda total, number: total * number,
89 numbers,
90 1
91 )
92 for symbol, numbers in possible_gears.items()
93 if len(numbers) > 1
94 ])
diff --git a/day3/example.txt b/day3/example.txt
new file mode 100644
index 0000000..b20187f
--- /dev/null
+++ b/day3/example.txt
@@ -0,0 +1,10 @@
1467..114..
2...*......
3..35..633.
4......#...
5617*......
6.....+.58.
7..592.....
8......755.
9...$.*....
10.664.598..
diff --git a/day3/input.txt b/day3/input.txt
new file mode 100644
index 0000000..c1073d9
--- /dev/null
+++ b/day3/input.txt
@@ -0,0 +1,140 @@
1.....613...................................439............498.........................438......617....343.............942...................
2.......*............790...........269..735..*........................../679..............*444.*.........*.......147...*.............441.....
3....539......422.......*......662*........*..691..........*124.15..675.................=.......404...872............237......930.....+......
4........334.............861.........%....479..........424.......+.@.......402.......314...905................833........*293................
5..........#.......................906.................*...............950.....................712...437.........*142.359........551.14......
6....509.....=...........890...................&........9................./..847.154..568............@...102................280...*..........
7.....*..950.67.............-......161.......530....=...................=............*...../..............@.......................426........
8614...............................*.............59.591.....259*.......342...427..........29../.........-....................946.............
9.......896........808..............6.............*.............35............./......538.....572....510...677*380...........=...............
10.......*.............*534..814................879...............................615/....*............................90.........580...413...
11.......810..........................................714.260..498..........386..........877......754........793...943.*............/.....*...
12.....................845..#..483........55...643.......*......&.....421..+.....+...527.....938............*.......*...993...............706.
13....339*..370..........-.540.@.....337..........*........39........*........404......&.......*...483...308.....567................../.......
14..........*......994..............*......855..356.768.....*.....114...@195..................976....-...............496.61...610...369.200...
15..........15........*...@890...447.........*.........*.303......................821..251*....................8........*....+................
16...962.............260...................@..716....893..............304............*.....537.........805.....#..........................388.
17............201*........................281.............471........*...../.....=....959...............%..238....180..641@...................
18....278.........132..........65...&.........*920.......*.......831..97..828.....533......#368............../.......*......66-...............
19.....*................*......*..627......343..........111.....*.....................................*.100$........693..........780..........
20...974........474..537.759.101.......366....................183...............461*178..850.......644.........57.............................
21..........67$.@.........................*...879..................54.100..355.................794.............=.....................826.530..
22494*549...............670...=194.....173............329=....*14....*........*....811*303....*....891..541........975........................
23........................*................403.............591................667............630....*.....$.667*....#...*......819.........19.
24572...@.......%.......271..........910.........578.551..........818.534...........................217.........809.....262.....+.............
25.....105.......748..............%........637...*...*........515....*...................119............*143..@.....258.......................
26.20........115........714....525...491....*..768...647.....*...................433..../............583.......404..#..............268........
27.......650..*.....%......%............*.311............275.283.306..=659.....................499........@................652.176............
28..100.....#.193.637..........828...806.............725*........*..............................@......418........370.......@........86&..977.
29......350................229..$.............................%...724.....#.981*582..$...............................*.38.....38.503......$...
30.......#..380...........*.............311........%..........968......647..........33..................634*13......72./...%...*..*...........
31............*.203*52.......+........./...........295.....................888..........277...938..662.........3.........619.320...766........
32....149...883..............817...........163............959..........8.....*.811%............*......&...354..+.....392......................
33....................727........789......*...............@...............739.........537....326.............*......*.......&925.........938..
34..383..505.166......-.........*.......565......491........136.....693..........870.%...........#...........654..&..313.................*....
35.........=..$..801....962.....787..........181......186.../..........*103........+............826....599.......289.....311.768..........986.
36.292.414..........*......*................*..................214.....................439.@130...........+...52........*.....*.....989*......
37.........221......149....405...@...........393..........714.....&.......652............*............607....*.........412....682.......982...
38.........*......................491.......................*..............-......906..909.......885.....%.747......19........................
39......567.........193........1..................925.........78......117.....797*................@............208............................
40.............943..*.....571...*.....919.487............/....+.........*.......................................*......433.922.......795......
41....66......*....92..........367.......*....883.625.....479....852@.............277.635............860.......940..21....*....995.....&......
42.........206............595%...................*............./.......................*......691.......*..........*..........*...............
43.....766.............&................=...........*...649-.241...56..@724..713..836.863.......=....676...+..824.506......247..$132..........
44.....................278..........917..864.....219...............+............%.*......................973.........................744......
45.........912...............%......*................979....-953...................733.............&................................*.........
46.........@........$........995....905......552...................391.........40=.......388.......141...................731*640.....511......
47123..662...989.672............................*........611......+.....%910.............*....5......................584......................
48...*...*../...........................306.....289.*....*.............................673.....*........970..51....@....*...............#.....
49...916.23.......14..828........908.................181.227......26....190.....723..*.......877...513...@...*..977......319..867/...997..76..
50..........158........=............$..............................%............#...605....................264.......569.....................1
51.....453..*............................815.430*........946..840.........................456.........152................23...................
52216.....*.389...........930............*.......211.......-...*.....635..472*185.................................@444...*.....783...966......
53...*...72.................*..........527...+...............636.......*.............746....575.....5...276.821........568.+......*...-.......
54..765...........991........686..142*......588......@...........366....395.............*..*...............................983...266..........
55.................+..................383.............330..&......%...............337.853.337........927.@703.&513......................855...
56...652................=../..@.............431....48......512...............541....%.................+.............-...835/.......%......*...
57.....*..205.....510.92..901.778............*......./............588.........#..........@....................968.603........820...75....844..
58...560............/........................597...................&.......#............698..........427........*...../249....................
59..............817..........50..........208..................149..........169........................*......835..............522.............
60.....-......................*............*.....470......%..*.......151.................731.......395..979.........200.........#.......$.....
61..242.........698........763............531.....*....252...533.....*.........64.........*...233.........*............*262.............498...
62........994.....*...............620.639......575................934..606.99....*....770.72.#......@.............................-...........
63.......*.........658..355.........#...*............*...*597..............*...645......*........292........-..354...@.......409...515........
64........453..594.......#..............571..=552.193...8.............820...69.........877....%..........846.....*...525.446....*........*386.
65....&...........#..................................................*....-.................652.................500............361....290.....
66...833..................652.....410..924*72..........*.../104.....231.223....@...%....775................375................................
67.................470...........*..................663.......................179.10....&.........256..770*............243.........354........
68...*5.............*............962......-....................../.....529...................../.....*.........39.255./.......73......&.......
69...............253..766....$.............659........$497..241.656....&....630................262.212...154.......*............*.............
70...................*....755...................375...........*................@...........................*.....936...159.......786...981....
71...................78....................426.....*39....885..835....432..............439.................335............@...................
72.......*574.785............................................*................736......*.....860....395...........381...........@..114...825..
73....557........@.............187.....640......514.36*.....834.........630...@....*..194......#....-......*......*....164....440.%.....*.....
74...................744.908....*........*......*......185......................165........635..........464.354..104.................996......
75.........................*.805..604..375...326........................*.................../....970.....................*....................
76....751.....776.......661.......*.................777*........434..952.662....2......................$.923...........99.463.........&.514...
77....*........*.....55..........414..............................*..............$......&522........378............+............*....89..*....
78.331.......403..94..................547.551..............300...600........=......452.......976.+...............213.........815.757..........
79.....209-......*...760...............@...*.............................963........+..........*.906.......785...........................+....
80958...........319....@.....+.334..........640.................................798.........611......678.....*..630.......389............426..
81.........../............961..*..................608..472...........45...........*...................*....390.............*..................
82.....597..191......&861.......279.689.............=....*.972*..........560......900.............548..97.......240...51....2....411.......233
83......*.......379.................*......+...........770.....989...955...&.214#.........*588.......*.............%..$.......................
84....982.&.......................82....850...450......................*...............755.....799....539.....298.........993........788......
85.........901....382.......569...............&........428............281.....=599.........98.../....................789.@......956.=....450..
86.................*..497/..*...664....*...............%.........610................/944.....*....778......713*375.....*.................*....
87.......@......637........443.../...905............................*883.......389...........563......................388...$.....*559....386.
88........241.......*597.........................876..............$........731*.....192.483..............#958...%..*......828..100............
89...89&.........994.........*228...............#...........18...413..............1....*...........*..........157...453.......................
90..........573...........198............447........488.672../.#......&383.........%.....589........511..................46...................
91.....*449......$...228*................+.............*........662.........%.............*....321........34%....838&...*.....................
92..592.......659...............=...........................................188..248....836......*....................141..%......344.........
93............................564....481..............-..........................................861............%...........93.....$...$.936..
94